package megrez.mail.sender;

import com.aliyun.dm20151123.models.SingleSendMailResponse;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import megrez.boot.autoconfigure.aliyun.mail.MailSenderClient;
import megrez.boot.autoconfigure.aliyun.mail.MailSenderMessage;
import megrez.mail.sender.common.MailSenderBean;
import megrez.mail.sender.common.MailSenderException;
import megrez.mail.sender.common.MailSenderService;

/**
 * 阿里云邮件推送服务
 * 
 * @author Lucky Yang
 * @since 2.6.9
 */
@Slf4j
@RequiredArgsConstructor
public class AliyunMailSenderService implements MailSenderService {
    private final MailSenderClient mailSenderClient;
    
    @Override
    public void send(MailSenderBean bean) {
        bean.validate();

        MailSenderMessage message = new MailSenderMessage();
        message.setBody(bean.getBody());
        message.getMailAddresses().addAll(bean.getMailAddresses());
        message.setSubject(bean.getSubject());
        message.setTextable(bean.isTextable());

        try {
            SingleSendMailResponse reponse = mailSenderClient.singleSendMail(message);
            if (log.isDebugEnabled()) {
                log.debug("Aliyun MailSenderClient reponse: {}", reponse);
            }
        } catch (Exception e) {
            throw new MailSenderException(e);
        }
        
    }
}
